<template>
  <div class="app-container">
    <!-- <el-calendar v-model="value" class="maxwidth-1000"></el-calendar> -->
    <el-card shadow="hover" class="ky-card ky-card-hdbg">
      <el-header>
        <el-row>
          <el-col class="info" :xs="24" :sm="24" :md="12" :xl="12">
            <img class="avatar" src="~@/assets/images/doctor.png"/>
            <!-- 有会诊权限（是医生） -->
            <div class="info-box">
              <p class="title">
                您好，{{$store.state.user.userName}}，祝您每日工作顺利！
              </p>
              <p class="content">
                <span>
                  {{$store.state.user.userInfo.deptName}} |
                  {{$store.state.user.userInfo.hospitalName}}
                </span>
              </p>
            </div>
            <!-- 助手（审核人）或者普通用户（申请人） -->
            <!-- <div class="info-box" style="margin:15px;">
            <p class="title">您好，{{$store.state.user.userName}}！</p>
          </div> -->
          </el-col>
          <el-col
            class="statistic"
            :xs="24"
            :sm="24"
            :md="12"
            :xl="12"
            v-loading="checkNumLoading"
          >
            <div class="num">
              <span class="num_yyy">{{checkRoomData.yyy}}</span> /
              {{checkRoomData.total}}
            </div>
            <div class="tips">当前医院：已预约/可预约</div>
          </el-col>
        </el-row>
      </el-header>
    </el-card>
    <div class="app_img_wrap">
      <div
        class="app_img_item"
        v-for="(app, index) in appList"
        :key="index"
        :style="{ backgroundColor: app.bgColor }"
        @click="gotoRouter(app)"
      >
        <i class="ico" :class="app.icon"></i>
        <div class="txt">{{app.title}}</div>
      </div>
    </div>
    <!-- <setprocess></setprocess> -->
  </div>
</template>

<script>
import { getLocalStorage, removeLocalStorage } from '@/utils/localStorage' // removeLocalStorage
import { getDataForCheckRoomChart } from 'api/api.js'
import { dateFormatter } from '@/utils/validate'
// import { getAppJson } from '@/utils/versionReload'
// import setprocess from '@/components/step/setprocess'
const apps = [
  {
    origin: true,
    title: '预约登记',
    desc: '检查预约登记',
    router: '/check/registration',
    icon: 'fa fa-table',
    bgColor: '#ff7600',
    permission: ['YJYY10008001', 'YJYY100070001'] // YJYY10008001 YJYY100070001
  },
  {
    origin: true,
    title: '预约记录',
    desc: '预约记录查询',
    router: '/check/sourceEnquiry',
    icon: 'fa fa-book',
    bgColor: '#009deb',
    permission: ['YJYY10008001', 'YJYY100070001'] // YJYY10008001 YJYY100070001
  },
  {
    origin: true,
    title: '预约概况',
    desc: '预约情况、限制预约原因查询！',
    router: '/schedule/limitAppointment',
    icon: 'fa fa-calendar',
    bgColor: '#EF8201',
    permission: 'YJYY10001006'
  },
  {
    origin: true,
    title: '号源维护',
    desc: '时段号源设置，号源数量、状态等',
    router: '/schedule/manage',
    icon: 'fa fa-th',
    bgColor: '#37D0C0',
    permission: 'YJYY10001002'
  },
  {
    origin: true,
    title: '模板维护',
    desc: '机房号源统一模板设置',
    router: '/appointmaintain/template',
    icon: 'fa fa-sliders',
    bgColor: '#0062B0',
    permission: 'YJYY10002031'
  },
  {
    origin: true,
    title: '预约规则',
    desc: '注：限制部位需与HIS电子申请单部位一致',
    router: '/appointmaintain/rulessetting',
    icon: 'fa fa-bars',
    bgColor: '#00aa88',
    permission: 'YJYY10002034'
  },
  {
    origin: true,
    title: '机房管理',
    desc: '机房的设置管理',
    router: '/business/roommanage',
    icon: 'fa fa-building-o',
    bgColor: '#00aa66',
    permission: 'YJYY10005001'
  }
]

export default {
  components: {
    // setprocess
  },
  data() {
    return {
      value: new Date(),
      aesToken: '',
      aesjToken: '',
      checkNumLoading: false,
      checkRoomData: {
        total: 0,
        yyy: 0,
        kyy: 0
      },
      checkRoomList: [],
      appList: []
    }
  },
  async created() {
    const resetRedirect = getLocalStorage('resetRedirect')
    if (resetRedirect) {
      // 第三方跳转带重定向路由
      removeLocalStorage('resetRedirect')
      this.$router.push(resetRedirect)
    }
    // 获取首页模块入口
    let tags = this.$store.state.user.tags
    let appList = []
    apps.forEach(app => {
      console.log('typeof app.permission', typeof app.permission)
      if (typeof app.permission == 'object') {
        let isHasPermission = false
        app.permission.forEach(item => {
          if (tags.indexOf(item) >= 0) {
            isHasPermission = true
          }
        })
        isHasPermission && appList.push(app)
      }
      if (typeof app.permission == 'string') {
        if (tags.indexOf(app.permission) >= 0) {
          appList.push(app)
        }
      }
    })
    this.appList = appList
  },
  mounted() {
    console.log('mounted_in_home', window.g)
    // 统计数据
    this.getCheckRoomData()
  },
  methods: {
    getCheckRoomData() {
      const params = {
        hospitalCode: this.$store.state.user.hospitalCode,
        dateArr: [
          dateFormatter(new Date(), false),
          dateFormatter(new Date(), false)
        ].join(',')
      }
      this.checkNumLoading = true
      getDataForCheckRoomChart(params).then(res => {
        this.checkNumLoading = false
        if (res && res.Code === 200) {
          this.checkRoomList = res.Content.BaseData
          let total = 0
          let yyy = 0
          this.checkRoomList.forEach(item => {
            total = total + item.KYY
            yyy = yyy + item.YYY
          })
          this.checkRoomData.total = total
          this.checkRoomData.yyy = yyy
          this.checkRoomData.kyy = total
        }
      })
    },
    // 路由
    gotoRouter(data) {
      const permission = data.permission
      const tags = this.$store.state.user.tags
      if (typeof permission == 'object') {
        let isHasPermission = false
        permission.forEach(item => {
          if (item && tags.indexOf(item) > -1) {
            isHasPermission = true
          }
        })
        if (!isHasPermission) {
          this.$message.warning('暂无权限！')
          return false
        }
      }
      if (typeof permission == 'string') {
        if (permission && tags.indexOf(permission) === -1) {
          this.$message.warning('暂无权限！')
          return false
        }
      }
      if (data.origin) {
        this.$router.push(data.router)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-header {
  background-color: #fff;
  position: relative;
  color: #333;
  text-align: left;
  height: 100% !important;
  padding: 0px;
  .info {
    padding: 5px 20px;
    display: inline-block;
    // width: 48%;
    .avatar {
      width: 50px;
      vertical-align: top;
      margin-top: 20px;
      display: inline-block;
    }

    .info-box {
      margin-left: 25px;
      width: 70%;
      display: inline-block;
      .title {
        font-size: 18px;
        font-weight: bold;
        margin: 16px 0;
      }
      .content {
        font-size: 14px;
        color: #7f7f7f;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .statistic {
    position: relative;
    .num {
      text-align: center;
      font-size: 20px;
      line-height: 95px;
      .num_yyy {
        font-size: 40px;
        color: #f00;
      }
    }
    .tips {
      font-size: 12px;
      color: #333;
      position: absolute;
      right: 0;
      bottom: 0;
      z-index: 1;
    }
  }
}

.el-aside {
  margin-left: 10px;
  color: #333;
  text-align: left;
  h3 {
    margin-top: 0px;
  }
  .message-box {
    height: 295px;
    background-color: #fff;
    margin-bottom: 10px;
    padding: 20px;
  }
  // line-height: 200px;
}

.el-main {
  background-color: #fff;
  color: #333;
  text-align: left;
  height: 600px;
  h3 {
    margin-top: 0px;
  }
  // line-height: 160px;
  .consulted-box {
    .consulted-title {
      color: #02a7f0;
      font-size: 14px;
      text-align: left;
      span {
        color: #7f7f7f;
      }
    }
    .consulted-content {
      border: 1px solid #ccc;
      display: inline-block;
      width: 120px;
      margin-right: 20px;
      .name {
        font-size: 16px;
        text-align: center;
      }
      .el-badge {
        width: 100%;
        .status {
          margin: 0;
          padding: 10px;
          background: #e9eef3;
          text-align: left;
        }
      }
    }
  }
}

body > .el-container {
  margin-bottom: 40px;
}

.app_img_wrap {
  display: flex;
  flex-wrap: wrap;
  padding: 10px 0;
  .app_img_item {
    padding: 20px 30px;
    text-align: center;
    width: 180px;
    background-color: #fff;
    border-radius: 10px;
    border: solid 1px #f9f9f9;
    cursor: pointer;
    margin: 10px;
    color: #fff;
    transition: all 0.1s linear;
    &:hover {
      transform: scale(1.05);
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    }
    .ico {
      font-size: 60px;
      padding: 30px;
    }
    .txt {
      // padding: 10px 0 0;
      font-size: 20px;
    }
  }
}

.el-badge >>> .el-badge__content.is-fixed {
  top: 20px;
  right: 35px;
}
.color-blue {
  color: #02a7f0;
}
.scroll {
  overflow-x: hidden;
  overflow-y: auto;
  height: 92%;
  padding: 0 10px 0 0;
}
</style>
